package com.jason.auto.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具类
 */
public class DateUtil {
    public static SimpleDateFormat format_date_1 = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat format_date_2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat format_date_3 = new SimpleDateFormat("HH:mm");

    public static String formatDate(Date date, SimpleDateFormat format) {
        return format.format(date);
    }

    public static Date parseDateFromStr(String time, SimpleDateFormat format) {
        Date date = new Date();
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 得到几天后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 计算一个固定时间多少分钟后的时间
     *
     * @param date
     * @param mins
     * @return
     */
    public static Date getDateAfterMin(Date date, int mins) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = mins / 60;
        int min = mins % 60;
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + min);
        return calendar.getTime();

    }

    /**
     * 获取今日的开始时间
     * <p>
     * eg:2017-07-31 00:00:00
     *
     * @return
     */
    public static Date getTodayStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getDayStartTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定日期为一年中的第几周
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取当前时间所在年的最大周数
     *
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    /**
     * 获取某年的第几周的开始日期
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年的第几周的结束日期
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 获取当前时间所在周的开始日期
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 获取当前时间所在周的结束日期
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 得到给定时间所在的月的最后一天
     *
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }

    /**
     * 得到给定时间所在的月的开始的那天
     *
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
        return calendar.getTime();
    }

    /**
     * 根据开始时间和结束时间计算周跨度
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String[]> getWeekRangeFromStEt(String startTime, String endTime) {
        List<String[]> list = new ArrayList<String[]>();
        Date startDate = getFirstDayOfWeek(parseDateFromStr(startTime, format_date_1));
        Date endDate = getLastDayOfWeek(parseDateFromStr(startTime, format_date_1));
        list.add(new String[]{formatDate(startDate, format_date_1), formatDate(endDate, format_date_1)});
        if (endDate.before(parseDateFromStr(endTime, format_date_1))) {
            list.addAll(getWeekRangeFromStEt(formatDate(getDateAfter(endDate, 1), format_date_1), endTime));
        }
        return list;
    }

    /**
     * 根据开始时间和结束时间计算月跨度
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String[]> getMonthRangeFromStEt(String startTime, String endTime) {
        List<String[]> list = new ArrayList<String[]>();
        Date startDate = getFirstDayOfMonth(parseDateFromStr(startTime, format_date_1));
        Date endDate = getLastDayOfMonth(parseDateFromStr(startTime, format_date_1));
        list.add(new String[]{formatDate(startDate, format_date_1), formatDate(endDate, format_date_1)});
        if (endDate.before(parseDateFromStr(endTime, format_date_1))) {
            list.addAll(getMonthRangeFromStEt(formatDate(getDateAfter(endDate, 1), format_date_1), endTime));
        }
        return list;
    }

    /**
     * 获取几分钟之后的实际
     *
     * @param date
     * @param min
     * @return
     */
    public static Date getNextMinsTime(Date date, int min) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.MINUTE, min);
        return nowTime.getTime();
    }

    public static List<String> getHourMinArr() {
        SimpleDateFormat format_date = new SimpleDateFormat("HH:mm");
        Date startTime1 = getTodayStartTime();
        Date startTime2 = getDateAfter(startTime1, 1);
        Date tempTime = startTime1;
        List<String> timeArr = new ArrayList<String>();
        while (true) {
            if (tempTime.after(DateUtil.getNextMinsTime(startTime2, -1))) {
                break;
            }
            timeArr.add(format_date.format(tempTime));
            tempTime = getNextMinsTime(tempTime, 1);
        }
        return timeArr;
    }
}